package com.lzy.crm.wechat.menu;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.lzy.crm.wechat.consts.WechatPayProperties;
import com.lzy.crm.wechat.util.WeixinUtil;

/**
 * 微信公众号项目
 *
 * File: TextMsg.java
 * 
 * Copyright (C): 2017
 *
 * Description: 自定义菜单(使用：createCommMenu()//常见自定义菜单
 *
 * @author 陈升平
 * Notes: TextMsg.java 2017-04-26 下午13:56:14 CHENSP
 */
@Service("myMenu")
public class MyMenu {
	
	// public static Map<String,String> MenuUrlmap=new HashMap<String,String>();  
	private static String HOSTNAME = WechatPayProperties.getMenuUrl()+"crm/wechat/";
	private static String GETCODEURL = HOSTNAME + "mp/code";

	
	private static String wxAuthUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?"
	 		+ "appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect";
	
	/**
	 *  LOG
	 */
	private static Logger logger = Logger.getLogger(MyMenu.class);
	
	/*public static String getMenuUrlById(String menuId){
		return MenuUrlmap.get(menuId);
	}*/

	/**
	 * 创建个性化菜单
	 */
	public void createCommMenu() {
		String createMenuStr = getMenuJson();
		logger.info(createMenuStr);
		System.out.println("公众号 菜单初始化~~~~");
		boolean result = WeixinUtil.createMenu(createMenuStr, 1); //1 是普通的自定义菜单，2 是个性化菜单（目前没有需求)
		if(result == false){
			logger.error("creat wechat menu failed!");
		}
		logger.info(createMenuStr);
		return;
	}

	/*
	 * 任务（注释：“m-0”表示父按钮；“m-n”表示第m个父按钮，第n个子按钮（m,n≠0））：1-0：名字：click，响应点击事件：点击推事件
	 * 。2-0：名
	 * 字:父按钮2。2-1：名字：view,响应事件：跳转网页；2-2：名字：scancode_push，响应事件：扫码推事件；2-
	 * 3：名字：scancode_waitmsg
	 * ，响应事件：扫码推事件且弹出“消息接收中”提示框；2-4：名字：pic_sysphoto，响应事件
	 * ：弹出系统拍照发图。2-5：名字：pic_photo_or_album，响应事件：弹出拍照或者相册发图。3-0：名
	 * 字:父按钮3。3-1：名字
	 * ：pic_weixin,响应事件：弹出微信相册发图器；3-2：名字：location_select，响应事件：弹出地理位置选择器
	 * ；3-3：名字：media_id
	 * ，响应事件：下发消息（除文本消息）；3-4：名字：view_limited，响应事件：跳转图文消息url。
	 */
	public String getMenuJson() {
		// JSON处理工具
		Gson gson = new GsonBuilder().disableHtmlEscaping().create();
		// 菜单类
		Menu menu = new Menu();
		// 菜单中的父按钮集合
		List<FatherButton> fatherButtons = new ArrayList<FatherButton>();
		
		// 父按钮1 TODO
		FatherButton fb1 = new FatherButton();
		List<SonButton> sonButtons88 = new ArrayList<SonButton>();
		SonButton sb18 = new SonButton();
		sb18.setName("游戏下载");
		sb18.setUrl(HOSTNAME + "game_list.html");
		sb18.setType("view");
		SonButton sb34 = new SonButton();
		sb34.setName("充值房卡");
		sb34.setUrl(HOSTNAME + "mall_list.html");
		sb34.setType("view");
		SonButton sb35 = new SonButton();
		sb35.setName("代理申请");
		sb35.setUrl(HOSTNAME + "player_to_user.html");
		sb35.setType("view");
		sonButtons88.add(sb18);
		//sonButtons88.add(sb34);
		sonButtons88.add(sb35);
		
		fb1.setName("游戏下载");
		fb1.setSonButtons(sonButtons88);
		
		
		//fb1.setUrl(HOSTNAME + "game_list.html");
		// MenuUrlmap.put("12", "initDevMge");
		//fb1.setType("view");
		
		
		// 父按钮2
		FatherButton fb2 = new FatherButton();
		fb2.setName("充值房卡");
		List<SonButton> sonButtons4 = new ArrayList<SonButton>();
		// 子按钮的集合
		SonButton sb19 = new SonButton();
		sb19.setName("领百万红包");
		sb19.setUrl(HOSTNAME + "player_invite.html");
		sb19.setType("view");
		SonButton sb20 = new SonButton();
		sb20.setName("人工客服");
		sb20.setType("click");
		sb20.setKey("kefu");
		sonButtons4.add(sb34);
		//sonButtons4.add(sb19);
		sonButtons4.add(sb20);
		fb2.setSonButtons(sonButtons4);
		
		/*List<SonButton> sonButtons4 = new ArrayList<SonButton>();
		SonButton sb35 = new SonButton();
		sb35.setName("代理申请");
		sb35.setUrl(HOSTNAME + "player_to_user.html");
		sb35.setType("view");
		sonButtons4.add(sb35);
		fb2.setSonButtons(sonButtons4);*/
		
		
		// 父按钮3
		FatherButton fb3 = new FatherButton();
		fb3.setName("用户中心");
		List<SonButton> sonButtons3 = new ArrayList<SonButton>();
		// 子按钮3-1
		SonButton sb31 = new SonButton();
		sb31.setName("我的建议");
		sb31.setUrl(HOSTNAME + "advice.html");
		sb31.setType("view");
		/*SonButton sb32 = new SonButton();
		sb32.setName("我的投诉");
		sb32.setUrl(HOSTNAME + "complain.html");
		sb32.setType("view");*/
		/*SonButton sb37 = new SonButton();
		sb37.setName("人工客服");
		sb37.setUrl(HOSTNAME + "player.html");
		sb37.setType("view");*/
		// 子按钮3-2
		SonButton sb37 = new SonButton();
		sb37.setName("我的二维码");
		//sb32.setUrl(HOSTNAME + "token.html");
		sb37.setType("click");
		sb37.setKey("img");
		SonButton sb38 = new SonButton();
		sb38.setName("代理后台");
		sb38.setUrl(WechatPayProperties.getMenuUrl() + "crm/wechat/login.html");
		sb38.setType("view");
		
		
		
		
		SonButton sb39 = new SonButton();
		sb39.setName("邀请领红包");
		sb39.setUrl(WechatPayProperties.getMenuUrl() + "crm/wechat/invite.html");
		sb39.setType("view");
		
		// 添加子按钮到子按钮队列
		//sonButtons3.add(sb32);
		//sonButtons3.add(sb37);
		SonButton sb40 = new SonButton();
		sb40.setName("苹果手机设置");
		sb40.setUrl(WechatPayProperties.getMenuUrl() + "crm/wechat/ios_guide.html");
		sb40.setType("view");
		
		SonButton sb41 = new SonButton();
		sb41.setName("我的俱乐部");
		sb41.setUrl(WechatPayProperties.getMenuUrl() + "crm/wechat/crowd_index.html");
		sb41.setType("view");
		
		sonButtons3.add(sb41);
		sonButtons3.add(sb37);
		sonButtons3.add(sb40);
		//sonButtons3.add(sb39);
		sonButtons3.add(sb38);
		sonButtons3.add(sb31);
		// 将子按钮放到3-0父按钮队列
		fb3.setSonButtons(sonButtons3);

		// 将父按钮加入到父按钮集合
		fatherButtons.add(fb1);
		fatherButtons.add(fb2);
		fatherButtons.add(fb3);

		// 将父按钮队列加入到菜单栏
		menu.setFatherButtons(fatherButtons);
		return gson.toJson(menu);
	}
	
	public static String makeWxAuthURL(String menuId){
		String url = wxAuthUrl;
		String EncodeUrl = null;
		logger.error("--------------------------makeWxAuthURL()---begin---------");
		try {
			EncodeUrl = URLEncoder.encode(GETCODEURL, "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		if(EncodeUrl ==  null){
			return null;
		}
		url = url.replace("APPID", WechatPayProperties.getAppId());
		url = url.replace("REDIRECT_URI", EncodeUrl);
		url = url.replace("SCOPE", "snsapi_base");
		url = url.replace("STATE", menuId);
		logger.error("--------------------------makeWxAuthURL()---end---"+url+"------");
		return url;
	}
}
